package com.jia.videos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.videos.common.dto.VideoQuery;
import com.jia.videos.common.dto.VideoSearch;
import com.jia.videos.entity.VideoShow;
import com.jia.videos.mapper.VideoShowMapper;
import com.jia.videos.service.VideoShowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2020-12-21
 */
@Service
public class VideoShowServiceImpl extends ServiceImpl<VideoShowMapper, VideoShow> implements VideoShowService {


    @Override
    public List<VideoShow> getIndexBanner() {

        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("banner",1).orderByDesc("create_time");
        wrapper.last("limit 4");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getIndexMovie() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",1L).eq("banner",1).orderByDesc("create_time");
        wrapper.last("limit 6");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getIndexTelevision() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",2L).eq("banner",1).orderByDesc("create_time");
        wrapper.last("limit 6");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getIndexCategory(Long categoryId) {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId).eq("recommend",1).orderByDesc("create_time");
        wrapper.last("limit 6");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getIndexHot() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("hot",1).orderByDesc("create_time");
        wrapper.last("limit 6");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public Map<String, Object> queryList(VideoQuery videoQuery) {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        Integer pageNum = videoQuery.getPageNum();
        Integer pageSize = videoQuery.getPageSize();
        String country = videoQuery.getCountry();
        String date = videoQuery.getDate();
        String type = videoQuery.getType();
        Long categoryId = videoQuery.getCategoryId();

        if(!StringUtils.isEmpty(categoryId)){
            wrapper.eq("category_id",categoryId);
        }
        if(!StringUtils.isEmpty(country)){
            wrapper.eq("country",country);
        }
        if(!StringUtils.isEmpty(date)){
            wrapper.eq("date",date);
        }
        if(!StringUtils.isEmpty(type)){
            wrapper.eq("type",type);
        }
        Page<VideoShow> videoShowPage = new Page<>(pageNum,pageSize);
        IPage<VideoShow> videoShowIPage = baseMapper.selectPage(videoShowPage, wrapper);
        long current = videoShowIPage.getCurrent();
        long pages = videoShowIPage.getPages();
        List<VideoShow> records = videoShowIPage.getRecords();
        long size = videoShowIPage.getSize();
        long total = videoShowIPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("current",current);
        map.put("pages",pages);
        map.put("records",records);
        map.put("size",size);
        map.put("total",total);

        return map;
    }

    @Override
    public List<VideoShow> getMovieBanner() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",1L).eq("banner",1).orderByDesc("create_time");
        wrapper.last("limit 4");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getMovieHotList() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",1L).eq("hot",1).orderByDesc("create_time");
        wrapper.last("limit 10");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getTelevisionBanner() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",2L).eq("banner",1).orderByDesc("create_time");
        wrapper.last("limit 4");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getTelevisionHotList() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",2L).eq("hot",1).orderByDesc("create_time");
        wrapper.last("limit 10");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getComicBanner() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",3L).eq("banner",1).orderByDesc("create_time");
        wrapper.last("limit 4");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getComicHotList() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",3L).eq("hot",1).orderByDesc("create_time");
        wrapper.last("limit 10");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getRecommend() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("recommend",1).orderByDesc("create_time");
        wrapper.last("limit 8");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> queryAll(String keys) {

        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(keys)){
            wrapper.like("name",keys).or().like("actor",keys);
        }
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);

        return videoShows;

    }

    @Override
    public List<VideoShow> getHots() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("hot",1).orderByDesc("create_time");
        wrapper.last("limit 12");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;
    }

    @Override
    public List<VideoShow> getMemberList() {
        QueryWrapper<VideoShow> wrapper = new QueryWrapper<>();
        wrapper.eq("free",0).eq("recommend",1).orderByDesc("create_time").last("limit 4");
        List<VideoShow> videoShows = baseMapper.selectList(wrapper);
        return videoShows;

    }

}
